var mongoose=require('mongoose');
var config=require('../config');
var Schema=mongoose.Schema;
var db=mongoose.createConnection(config.db_url);
db.on('error',function(error){
    db.close();
    console.log(error);
});

//db.on('close', function () {
//    db.open(config.db_url);
//    console.log("重新连接mongo");
//});

var faultSchema = new Schema({
    sim: String,
    date: Date, //故障时间
    ftype: String, //故障类型
    content:String //故障内容
});
//console.log("进入fault.js");
var Fault = db.model("Fault", faultSchema);

Fault.prototype.findAllSim=function(callback){
    Fault.find(function(err,datas){
        return callback(datas);
    })
}

Fault.prototype.findForPage=function(obj,callback){
    var q=obj.search||{};                                       //查询条件
    var col=obj.columns||{};                                    //返回字段
    var query=Fault.find(q,col).skip(obj.page.skipFrom).limit(obj.page.limit);

    query.exec(function(err, datas) {
        if (err) {
            callback(err, null, null);
        } else {
            Fault.count(q, function(err, count) {
                if (err) {
                    callback(err, null, null);
                } else {
                    var pageCount = Math.ceil(count / obj.page.limit);
                    callback(null,count,pageCount, datas);
                }
            });
        }
    });
}
module.exports = Fault;
